package com.esun.rabbit2.viewer.mainact.optionact;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.option.OptionActFeedbackCommunicater;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @Title: UserFeedbackAct.java
 * @Package com.esun.rabbit2.viewer.mainact.optionact
 * @Description: (用户反馈)
 * @author Billy qigs@500wan.com
 * @date 2011-5-24 下午04:03:17 copy right 深圳市易讯天空网络技术有限公司
 */
public class UserFeedbackAct extends Activity implements OnClickListener {
	private ImageButton btnBack;// 返回按钮
	private Button btnPostMsg;// 反馈发送按钮
	private EditText edtMsg;// 反馈信息
	private OptionActFeedbackCommunicater feedBackCommunicater;
	private Handler handlerFeedBack;
	private static final int FEEDBACK_SUCCEED = 10001;
	private static final int FEEDBACK_FAILED = 10002;
	private ProgressDialog progressDialog;
	private String serverMessage;
	private String strFeedBackMsg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.option_userfeedback_act);

		initUI();// 初始化
		initHandler();
	}

	/* 按钮监听 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.optionuserfeedback_btn_postMsg:
			if (edtMsg.getText() != null && edtMsg.getText().toString().trim() != "" && edtMsg.getText().toString().length() != 0) {
				EDebug.o(this, "before encode = " + edtMsg.getText().toString());
				strFeedBackMsg = TextUtils.htmlEncode(edtMsg.getText().toString());
				EDebug.o(this, "after ecode = " + strFeedBackMsg);
				progressDialog.show();
				postMsgThread();
				EDebug.o(this, edtMsg.getText().toString());
			} else {
				Toast.makeText(UserFeedbackAct.this, getResources().getString(R.string.need_suggest), Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.optionuserfeedback_btn_back:
			UserFeedbackAct.this.finish();
			break;
		default:
			break;
		}
	}

	/* 初始化 */
	public void initUI() {
		btnBack = (ImageButton) findViewById(R.id.optionuserfeedback_btn_back);
		btnBack.setOnClickListener(this);

		btnPostMsg = (Button) findViewById(R.id.optionuserfeedback_btn_postMsg);
		btnPostMsg.setOnClickListener(this);

		progressDialog = ViewerUtil.showProgressDialog(UserFeedbackAct.this, getResources().getString(R.string.feedbacking));

		edtMsg = (EditText) findViewById(R.id.optionuserfeedback_edt_msg);
		edtMsg.setText("");
		edtMsg.setFocusable(true);

		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
	}

	/* 上传反馈信息 */
	public void postMsgThread() {

		new Thread() {

			@Override
			public void run() {
				if (feedBackCommunicater == null) {
					feedBackCommunicater = new OptionActFeedbackCommunicater();
				}
				Message msg = new Message();
				try {
					boolean isSucceed = feedBackCommunicater.isFeedBack(strFeedBackMsg, getResources().getString(R.string.version));
					serverMessage = feedBackCommunicater.getMsgServer();
					if (isSucceed) {
						msg.what = FEEDBACK_SUCCEED;
					} else {
						msg.what = FEEDBACK_FAILED;
					}
					handlerFeedBack.sendMessage(msg);
				} catch (Exception e) {
					msg.what = Config.CONNECTION_FAIL;
					handlerFeedBack.sendMessage(msg);
				}
			}
		}.start();
	}

	public void initHandler() {
		handlerFeedBack = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				progressDialog.cancel();
				switch (msg.what) {
				case FEEDBACK_SUCCEED:

					ViewerUtil.showTipDialog(UserFeedbackAct.this, getResources().getString(R.string.post_sucess), true);

					break;

				case FEEDBACK_FAILED:

					ViewerUtil.showTipDialog(UserFeedbackAct.this, serverMessage, false);

					break;

				case Config.CONNECTION_FAIL:

					// ViewerUtil.showTipDialog(UserFeedbackAct.this, Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(UserFeedbackAct.this, Language.STR_CONNECTIONFAIL.toString(), Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
			}
		};
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}
}
